#!/bin/bash
###############################################################################
# @用例ID: 20230527-184850-057552086
# @用例名称: patch-test
# @用例级别: 3
# @用例标签:
# @用例类型: 功能
###############################################################################
[ -z "$TST_TS_TOPDIR" ] && {
    TST_TS_TOPDIR="$(realpath "$(dirname "$0")/..")"
    export TST_TS_TOPDIR
}
source "${TST_TS_TOPDIR}/tst_common/lib/common.sh" || exit 1
###############################################################################

g_tmpdir="$(mktemp -d)"

tc_setup() {
    msg "this is tc_setup"
    # @预置条件: 系统正常运行，且已安装patch包
    yum install -y patch
    assert_true [ $? -eq 0 ]
    return 0
}

do_test() {
    msg "this is do_test"

    # @测试步骤:1: 使用补丁包升级文件1，输出为文件3,且设置剥离级别为0
    # @预期结果:1: 成功升级文件，且文件3与文件2的内容相同
    echo "This is the firstfile" > file1
    assert_true [ $? -eq 0 ]
    echo "Thisisthesecondfile" > file2
    assert_true [ $? -eq 0 ]
    diff file1 file2 > testfile.patch
    assert_true [ $? -eq 0 ]

    patch -o file3 -p0 file1 testfile.patch
    assert_true [ $? -eq 0 ]
    cmp -s file3 file2
    assert_true [ $? -eq 0 ]

    
    # @测试步骤:2: 使用补丁包升级文件1，并备份文件1
    # @预期结果:2: 成功升级文件，且成功备份
    patch -b file1 testfile.patch
    assert_true [ $? -eq 0 ]
    cmp -s file1 file2
    assert_true [ $? -eq 0 ]
    [ -f file1.orig ]
    assert_true [ $? -eq 0 ]    


    return 0

    
}

tc_teardown() {
    msg "this is tc_teardown"
    rm file1 || return 1
    rm file2 || return 1
    rm file3 || return 1
    rm file1.orig || return 1
    rm testfile.patch || return 1
    rm -rfv "$g_tmpdir" || return 1
    return 0
}

###############################################################################
tst_main "$@"
###############################################################################
